package it.antonio.ilpizze.db.index.btree.memory;
import it.antonio.ilpizze.db.index.btree.Entry;
import it.antonio.ilpizze.db.index.btree.LeafNode;
import it.antonio.ilpizze.func.F;
import it.antonio.ilpizze.func.Reducer;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;



public class MemoryLeafNode<K extends Comparable<K>, V> implements LeafNode<K,V>{
	
	private K key;
	private int degree;
	private List<Entry<K,V>> entries = new ArrayList<Entry<K,V>>();
	
	private LeafNode<K,V> next;
	private LeafNode<K,V> previous;
	
	public MemoryLeafNode(K key, int degree) {
		this.key = key;
		this.degree = degree;
	}
	
	@Override
	public K key() {
		return key;
	}

	@Override
	public boolean isLeaf() {
		return true;
	}

	@Override
	public boolean isFull() {
		return entries.size() >= degree;
	}
	@Override
	public boolean isEmpty() {
		return entries.isEmpty();
	}

	@Override
	public Entry<K, V> get(int i) {
		return entries.get(i);
	}

	@Override
	public void insert(int index, Entry<K, V> entry) {
		
		entries.add(index, entry);
		
	}

	@Override
	public int size() {
		return entries.size();
	}

	@Override
	public Iterator<Entry<K, V>> iterator() {
		return entries.iterator();
	}
	
	@Override
	public String toString() {
		return "Leaf("+key+"){ " + F.reduce(entries ,new StringBuffer(), new Reducer<StringBuffer, Entry<K,V>>() {

			@Override
			public StringBuffer reduce(StringBuffer accumalator, Entry<K, V> obj) {
				return accumalator.append(obj+ " ");
			}
		}) + "}";
	}

	@Override
	public LeafNode<K, V> next() {
		return next;
	}

	@Override
	public LeafNode<K, V> previous() {
		return previous;
	}

	@Override
	public void next(LeafNode<K, V> next) {
		this.next = next;
	}

	@Override
	public void previous(LeafNode<K, V> previous) {
		this.previous = previous;
	}

	

}